package uk.ac.ebi.bioinvindex.model.impl;

import org.hibernate.search.annotations.*;
import uk.ac.ebi.bioinvindex.model.impl.xref.InvestigationXrefImpl;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * @author brandizi
 *
 */
@Entity
@Table (name = "investigation")
@Indexed (index = "bii")
public class InvestigationImpl extends HasReferencesImpl<InvestigationXrefImpl>{
	@Fields({
		@Field(index = Index.TOKENIZED, store = Store.YES),
		@Field(name = "all", index = Index.TOKENIZED)
	})
	private String title;

	@Fields({
		@Field(index = Index.TOKENIZED, store = Store.YES),
		@Field(name = "all", index = Index.TOKENIZED)
	})
	private String description;

	private Date submissionDate, releaseDate;
	private Collection<StudyImpl> studies = new ArrayList<StudyImpl>();
	private Collection<ContactImpl> contacts = new ArrayList<ContactImpl>();
	private Collection<PublicationImpl> publications = new ArrayList<PublicationImpl>();


	protected InvestigationImpl () {
		super ();
	}

	public InvestigationImpl (String title) {
	  super ();
	  this.title = title;
  }


	public String getTitle () {
  	return title;
  }

	public void setTitle (String title) {
  	this.title = title;
  }

	public String getDescription () {
  	return description;
  }

	public void setDescription (String description) {
  	this.description = description;
  }

	@Temporal(TemporalType.DATE)
	public Date getSubmissionDate () {
  	return submissionDate;
  }

	public void setSubmissionDate (Date submissionDate) {
  	this.submissionDate = submissionDate;
  }

	@Temporal(TemporalType.DATE)
	public Date getReleaseDate () {
  	return releaseDate;
  }

	public void setReleaseDate (Date releaseDate) {
  	this.releaseDate = releaseDate;
  }


	@OneToMany(targetEntity = uk.ac.ebi.bioinvindex.model.impl.ContactImpl.class,
			mappedBy = "investigation")
	public Collection<ContactImpl> getContacts() {
		return contacts;
	}

	protected void setContacts(Collection<ContactImpl> contacts) {
		this.contacts = contacts;
	}

	public void addContact(ContactImpl contact) {
		contacts.add(contact);
	}

	@OneToMany(
			targetEntity = PublicationImpl.class,
			mappedBy = "investigation", cascade = CascadeType.ALL
	)
	public Collection<PublicationImpl> getPublications() {
		return this.publications;
	}

	public void addPublication(PublicationImpl publication) {
		this.publications.add(publication);
	}

	protected void setPublications(Collection<PublicationImpl> publications) {
		this.publications = publications;
	}

	@OneToMany(
			targetEntity = InvestigationXrefImpl.class,
			mappedBy = "owner",
			cascade = CascadeType.ALL
	)
	public Collection<InvestigationXrefImpl> getXrefs () {
		return super.getXrefs ();
	}

	@ManyToMany( mappedBy = "investigations", targetEntity = StudyImpl.class )
	@JoinTable( name = "STUDY2INVESTIGATION" )
	public Collection<StudyImpl> getStudies () {
		return studies;
  }

	protected void setStudies (Collection<StudyImpl> studies) {
  	this.studies = studies;
  }

	public void addStudy ( StudyImpl study ){
		this.studies.add ( study );
	}

	@Override
	public String toString()
	{
		return String.format (
			"InvestigationImpl{ id = %s, accession = '%s', title = '%s', description = '%s', submitted = %s, released = %s, " +
			"contacts = '%s', publications = '%s, Xrefs = %s' }",
			getId(), getAcc (), getTitle(), getDescription (), getSubmissionDate (), getReleaseDate (), getContacts (),
			getPublications (), getXrefs ()
		);
	}

}
